package controller.expertInfo.leader2Check;

import base.response.OffsetLimitPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.LeaderCheckServices;
import servicesImp.expertInfo.LeaderCheckUtil;
import wt.entity.expertInfo.LeaderCheck;
import wt.entity.expertInfo.LeaderCheckExample;
import wt.entity.expertInfo.LeaderCheckWithBLOBs;
import wt.staticData.ShortData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller

@RequestMapping("expertInfo/leader2Check")
public class Leader2CheckController {
    @Autowired
    LeaderCheckServices leaderCheckServices;


    @RequiresPermissions("expertInfo:leader2Check:default")
    @RequestMapping("index")
    public String index() {
        return "expertInfo/leader2Check/index";
    }


    @RequiresPermissions("expertInfo:leader2Check:default")
    @RequestMapping("select")
    @ResponseBody
    public OffsetLimitPage select(String expertName, String entityName, Integer offset, Integer limit) throws Exception {

        LeaderCheckExample leaderCheckExample = new LeaderCheckExample();
        leaderCheckExample.setOrderByClause("id desc");
        LeaderCheckExample.Criteria criteria = leaderCheckExample.createCriteria();
        criteria.andWorkFlowStepEqualTo(ShortData.s3);
        if (!StringUtils.isBlank(expertName)) {
            criteria.andExpertNameLike("%" + expertName.trim() + "%");
        }

        if (!StringUtils.isBlank(entityName)) {
            criteria.andTableNameEqualTo(entityName);
        }

        OffsetLimitPage offsetLimitPage = leaderCheckServices.selectByExample(leaderCheckExample, offset, limit);
        return offsetLimitPage;
    }

    @RequiresPermissions("expertInfo:leader2Check:default")
    @RequestMapping("setResult")
    @ResponseBody
    public CommonResult setResult(String id, boolean result, String reason) throws Exception {
        CommonResult commonResult = new CommonResult();

        LeaderCheck leaderCheck = leaderCheckServices.selectByPrimaryKey(id);
        if (leaderCheck.getWorkFlowStep() != ShortData.s3) {
            commonResult.setMessage("数据已被处理");
            return commonResult;
        }

        if (result == false && StringUtils.isBlank(reason)) {
            commonResult.setMessage("请说明原因");
            return commonResult;
        }

        if (result) {
            LeaderCheckUtil.verifier3CheckTrue(leaderCheck);
        } else {
            LeaderCheckUtil.verifier3CheckFalse(leaderCheck, reason);
        }
        commonResult.setStatus(leaderCheckServices.updateByPrimaryKey(leaderCheck));

        return commonResult;
    }
//

    @RequiresPermissions("expertInfo:leader2Check:default")
    @RequestMapping("setResultList")
    @ResponseBody
    public CommonResult setResultList(@RequestParam("ids[]") List<String> idList) throws Exception {
        CommonResult commonResult = new CommonResult();
        LeaderCheck leaderCheck = new LeaderCheckWithBLOBs();

        LeaderCheckUtil.verifier3CheckTrue(leaderCheck);


        LeaderCheckExample leaderCheckExample = new LeaderCheckExample();
        leaderCheckExample.createCriteria().andWorkFlowStepEqualTo(ShortData.s3).andIdIn(idList);
        int size = leaderCheckServices.countByExample(leaderCheckExample);
        if (idList.size() != size) {
            commonResult.setMessage("请刷新页面！");
            return commonResult.setError();
        }
        int count = leaderCheckServices.updateByExampleSelective((LeaderCheckWithBLOBs) leaderCheck, leaderCheckExample);
        commonResult.setStatus(count > 0 ? 1 : 0);
        return commonResult;
    }



    @RequiresPermissions("expertInfo:leader2Check:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        LeaderCheckExample leaderCheckExample = new LeaderCheckExample();
        LeaderCheckExample.Criteria criteria = leaderCheckExample.createCriteria();
        criteria.andWorkFlowStepEqualTo(ShortData.s3);
        int count = leaderCheckServices.countByExample(leaderCheckExample);
        if (count == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", count + "条专家信息待复查");
            map.put("url", "expertInfo/leader2Check/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }



}
